package com.sgb.kjwl.view.ui.activity.self.authentication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCertificationBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.user.AuthDetailEntity;
import com.sgb.kjwl.viewmodel.self.CertificationModel;
import com.sgb.link.router.PathUser;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import javax.inject.Inject;
@Route(path = PathUser.CertificationActivity)
public class CertificationActivity extends AppActivity implements View.OnClickListener {
    private ActivityCertificationBinding binding;
    @Inject
    CertificationModel viewModel;
    private String type;
    private static final String KEY_ENTITY = "key_entity";
    public static final String KEY_DATA = "key_data";
    private AuthDetailEntity mEntity, upDateEntity;
    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, CertificationActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }
    public static void start(Context context,String type) {
        Intent intent = new Intent(context, CertificationActivity.class);
        intent.putExtra("type",type);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, String type, AuthDetailEntity entity) {
        Intent intent = new Intent(context, CertificationActivity.class);
        intent.putExtra("type",type);
        intent.putExtra(KEY_ENTITY, entity);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void startActivityResult(Activity activity, String type, AuthDetailEntity entity, int requestCode) {
        Intent intent = new Intent(activity, CertificationActivity.class);
        intent.putExtra("type",type);
        intent.putExtra(KEY_ENTITY, entity);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        if (getIntent() != null){
            type = getIntent().getStringExtra("type");
            mEntity = (AuthDetailEntity) getIntent().getSerializableExtra(KEY_ENTITY);
        }
        binding = DataBindingUtil.setContentView(CertificationActivity.this, R.layout.activity_certification);
        binding.setViewModel(viewModel);
        binding.include.actionBarTitle.setText(getResources().getString(R.string.real_auth));
        binding.include.actionBarImageLeft.setVisibility(View.VISIBLE);
        binding.authLl.setVisibility(View.VISIBLE);
        binding.include.actionBarImageLeft.setOnClickListener(this);
        binding.tvCommit.setOnClickListener(this);
        binding.knowTv.setOnClickListener(this);
//        binding.itemTvIdcard.setInputType(ItemInputTextView.TYPE_INDO);
        if (mEntity != null){
           binding.itemTvName.setContent(mEntity.getRealName());
           binding.contentEdtId.setText(mEntity.getIdCard());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.action_bar_image_left:
                complate();
                break;

            case R.id.know_tv:
                complate();
                break;

            case R.id.tv_commit:
                if (TextUtils.isEmpty(binding.itemTvName.getContent())) {
                    MToast.showToast(CertificationActivity.this, R.string.real_auth_name_hint);
                } else if (TextUtils.isEmpty(binding.contentEdtId.getText().toString())) {
                    MToast.showToast(CertificationActivity.this, R.string.real_auth_idcard_hint);
                } else {
                    viewModel.commitAuth(binding.itemTvName.getContent(), binding.contentEdtId.getText().toString(),type);
                }
                break;
        }
    }

    private void complate() {
        finish();
//        AppManagerHelper.getAppManagerHelper().finishActivity(OpenTicksModel.class);
//        AppManagerHelper.getAppManagerHelper().finishActivity(SupplierAuthActivity.class);
//        AppManagerHelper.getAppManagerHelper().finishActivity(AuthCenterActivity.class);
    }

    public void finishAndData() {
        if (mEntity != null){
            Intent intent = new Intent();
            upDateEntity = new AuthDetailEntity();
            upDateEntity.setRealName(binding.itemTvName.getContent());
            upDateEntity.setIdCard(binding.contentEdtId.getText().toString());
            intent.putExtra(KEY_DATA, upDateEntity);
            setResult(RESULT_OK, intent);
        }
        finish();
    }
}
